﻿create or replace function insertDistributor(
	out xml, inout xml
	)
as
$$
	declare
		-- alias is name for stored procedure parameters
		executeStatusData 	alias for $1;
		distributorData		alias for $2;
		
		-- variable for node data
		vIdDistributor 		bigint := null; -- distributor id in database
		vNameDistributor	text := 'ERROR'; 	-- distributor name
		
		-- node path
		vIdNode						varchar(100) := '/distributor/@id';
		vNameNode					varchar(100) := '/distributor/distributorname';
		
		
	begin
		-- get data from xml
		vIdDistributor := cast(extract_value(vIdNode, distributorData) as bigint);
		vNameDistributor := extract_value(vNameNode, distributorData);
		
		-- insert data into table
		insert into Distributor(
				nameDistributor)
			values(
				vNameDistributor)
			returning idDistributor into vIdDistributor;
			distributorData := distributor2Xml(vIdDistributor, vNameDistributor);
			
		executeStatusData := statusCall(successcode(), insertText()); -- status data
		return;

	end;
$$ language plpgsql VOLATILE;